#include<bits/stdc++.h>
#define endl '\n'
#define debug(x) cerr<<#x<<':'<<x<<endl
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const int N=2e5+5;
int n;
int a[N],b[N],k[N];
int f[N<<1];
int vis[N<<1];
int cnt[N<<1];
int ans;
int find(int x){
    if(x==f[x]) return x;
    else return f[x]=find(f[x]);
}
signed main(){
    freopen("pairint.in","r",stdin);
    freopen("pairint.out","w",stdout);
    IOS;
    cin>>n;
    for(int i=1;i<=2*n;i++) f[i]=i;
    for(int i=1;i<=n;i++){
        cin>>a[i]>>b[i];
        if(++cnt[a[i]]==1) ans++;
        if(++cnt[b[i]]==1) ans++;
        int u=find(a[i]),v=find(b[i]);
        if(u==v) continue;
        f[u]=v,k[i]=1;
    }
    for(int i=1;i<=n;i++){
        if(k[i]) continue;
        int u=find(a[i]),v=find(b[i]);
        if(u==v) vis[u]=1;
    }
    for(int i=1;i<=2*n;i++){
        if(!cnt[i]) continue;
        int u=find(i);
        if(u!=i) continue;
        ans+=vis[i]-1;
    }
    cout<<ans;
    return 0;
}